package com.angysnail.jdk.stream;

import java.util.List;

import com.google.common.collect.Lists;

public class StreamTest {

	public static void main(String[] args) {
		testParalle();
	}

	private static List<String> _prepare() {
		List<String> list = Lists.newArrayList("string1", "string2", "string3", "string4",
				"string5", "string6", "string7");
		return list;
	}

	private static void testParalle() {
		List<String> list = _prepare();
		long now1 = System.currentTimeMillis();
		Lists.newArrayList(list).parallelStream().forEach(s -> {
			System.out.println(Thread.currentThread() + "|" + s);
		});
		System.out.println(System.currentTimeMillis() - now1);
		long now2 = System.currentTimeMillis();
		Lists.newArrayList(list).parallelStream().forEachOrdered(s -> {
			System.out.println(Thread.currentThread() + "|" + s);
		});
		System.out.println(System.currentTimeMillis() - now2);
		long now3 = System.currentTimeMillis();
		Lists.newArrayList(list).stream().forEach(s -> {
			System.out.println(Thread.currentThread() + "|" + s);
		});
		System.out.println(System.currentTimeMillis() - now3);
		// =====================================
		/**
		 * <code>
		 * Thread[ForkJoinPool.commonPool-worker-2,5,main]|string7
		 * Thread[ForkJoinPool.commonPool-worker-3,5,main]|string1
		 * Thread[main,5,main]|string5
		 * Thread[ForkJoinPool.commonPool-worker-1,5,main]|string2
		 * Thread[ForkJoinPool.commonPool-worker-1,5,main]|string4
		 * Thread[ForkJoinPool.commonPool-worker-3,5,main]|string3
		 * Thread[ForkJoinPool.commonPool-worker-2,5,main]|string6
		 * 
		 * 65
		 * 
		 * Thread[ForkJoinPool.commonPool-worker-1,5,main]|string1
		 * Thread[ForkJoinPool.commonPool-worker-2,5,main]|string2
		 * Thread[ForkJoinPool.commonPool-worker-2,5,main]|string3
		 * Thread[ForkJoinPool.commonPool-worker-2,5,main]|string4
		 * Thread[ForkJoinPool.commonPool-worker-2,5,main]|string5
		 * Thread[ForkJoinPool.commonPool-worker-2,5,main]|string6
		 * Thread[ForkJoinPool.commonPool-worker-2,5,main]|string7
		 * 
		 * 4
		 * 
		 * Thread[main,5,main]|string1 
		 * Thread[main,5,main]|string2
		 * Thread[main,5,main]|string3 
		 * Thread[main,5,main]|string4
		 * Thread[main,5,main]|string5 
		 * Thread[main,5,main]|string6
		 * Thread[main,5,main]|string7
		 * 
		 * 1
		 * </code>
		 * 
		 * 结论：线程会自动关闭，顺序可以保证。第一个stream时间会长，随后的会小
		 */
	}
}
